import 'package:flutter/material.dart';
import 'package:oneday_calendar/Utils/FlutterToNativeUtil.dart';
import '../pages/HomePage.dart';
import '../pages/LeftDrawerPage.dart';
import '../model/UserModel.dart';
import '../pages/LoginPage.dart';

class MyTabController extends StatefulWidget {
  @override
  createState() => new MyTabState();
}

class MyTabState extends State<MyTabController>  {

  int _tabIndex = 0;

  List _tabViewArr = [
    HomePage(),
    Icon(Icons.local_florist, size: 128.0, color: Colors.black12,),
  ];

  void _didClickTabAtIndex(int index) {
    setState(() {
      _tabIndex = index;
    });
  }

  // 自定义导航栏左侧按钮
  Widget _leadingBtn (BuildContext context) {
    return IconButton(
      icon: ClipRRect(
        child: Image.asset('images/nav_user.png', fit: BoxFit.contain, width: 28, height: 28),
        borderRadius: BorderRadius.circular(28/2),
      ),
      onPressed: (){
//        if (User.instance.userID.length != null) {
          Navigator.of(context).push(
            new MaterialPageRoute(builder: (BuildContext context) {
              return new LoginPage();
            })
          );
//        } else {
//          Scaffold.of(context).openDrawer();
//        }
      },
    );
  }

  // 导航栏下方选项卡
  Widget _bottomBar () {
    return PreferredSize(
      preferredSize: const Size.fromHeight(40.0),
      child: new Theme(
        data: Theme.of(context),
        child: new Container(
            child: Column (
              children: <Widget>[
                Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: <Widget>[
                    FlatButton(
                      onPressed: (){
                        _didClickTabAtIndex(0);
                      },
                      child: Text('Me'),
                      padding: EdgeInsets.fromLTRB(80, 0, 0, 0),
                      textColor: _tabIndex == 0 ? Colors.black : Colors.black38,
                    ),
                    FlatButton(
                      onPressed: (){
                        _didClickTabAtIndex(1);
                      },
                      child: Text('All'),
                      textColor: _tabIndex == 1 ? Colors.black : Colors.black38,
                      padding: EdgeInsets.fromLTRB(10, 0, 0, 0),
                    ),
                  ],
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: <Widget>[
                    Container(
                      margin: EdgeInsets.fromLTRB(0, 0, _tabIndex == 0? 82 : 25, 3),
                      color: Colors.black,
                      width: 30,
                      height: 2,
                    )
                  ],
                )     // 下划线
              ],
            )
        ),
      ),
    );
  }

  Widget _myBar () {
    return AppBar(
      title: Image.asset('images/nav_title.png', fit: BoxFit.contain, width: 110, height: 42),
      leading: _leadingBtn(context),
      bottom: _bottomBar(),
    );
  }

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 3,
      child: Scaffold(
        backgroundColor: Colors.grey[100],
        appBar: _myBar(),
        body: _tabViewArr[_tabIndex],
        drawer: LeftDrawer()
      ),
    );
  }
}

